<template>
  <div>
    <div class="nav">
      <van-nav-bar
        title="聊天室"
        @click-left="onClickLeft"
        left-text="返回"
        left-arrow
      >
        <template #right>
          <van-tag plain type="primary">在线人数{{ pel }}人</van-tag>
        </template>
      </van-nav-bar>
    </div>

    <div class="main" id="mainbox">
      <div class="center">
        <div class="inner" v-if="biaoji == 1">{{ welMsg }}</div>
      </div>
      <div v-for="(item, index) in chat" :key="index">
        <div class="left msg" v-if="item.userID != emailLogin[0].userID">
          <img :src="iconUrl + item.heardImg" />
          <div class="nr">
            <div class="neir">{{ Decode(item.neir) }}</div>
            <div class="nt">
              {{ item.userName }} {{ changetime(item.time) }}
            </div>
          </div>
        </div>
        <div class="right msg" v-if="item.userID == emailLogin[0].userID">
          <img :src="iconUrl + item.heardImg" />
          <div class="nr">
            <div class="neir">{{ Decode(item.neir) }}</div>
            <div class="nt">{{ changetime(item.time) }}</div>
          </div>
        </div>
      </div>
    </div>
    <!-- <div style="height: 5.5rem" class="fbox"></div> -->
    <div style="height: 5.5rem" class="fbox"></div>
    <div class="send">
      <div>
        <img
          src="https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/biaoqing.png"
          @click="showEmojin = !showEmojin"
          class="emjonBtn"
          alt=""
        />
      </div>
      <textarea
        id="emojiInput"
        ref="texttemplate"
        v-model="emojitext"
        :auto-size="{ minRows: 6, maxRows: 15 }"
        placeholder="请输入内容"
        allow-clear
        class="chat-send"
      />
      <img @click="send" class="sendimg" src="../../assets/images/send.png" />
      <div>
        <img
          src="https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/jiaicon.png"
          class="emjonBtn emjonadd"
          alt=""
        />
      </div>
      <transition name="van-slide-up">
        <div class="Picker" v-if="showEmojin">
          <VEmojiPicker
            style="width: 100%; height: 422px"
            @select="selectEmoji"
          />
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import myfun from "../../utils/timeDate.js";

import { mapState } from "vuex";
import io from "socket.io-client";
let Base64 = require("js-base64").Base64;
export default {
  sockets: {
    connection() {
      console.log("链接成功");
    },
    msg(data) {
      console.log(data);
    },
  },
  data() {
    return {
      showEmojin: false,
      emojitext: "",
      pel: 0,
      chat: [],
      welMsg: "",
      biaoji: 1,
      emjon: "",
      chat1: [
        {
          name: "小明",
          neir: "sdsagfegfdgsf",
          img: "../../assets/images/j.png",
          time: new Date(),
          id: 11,
        },
        {
          name: "欢迎小明",

          id: 3,
        },
        {
          name: "小明",
          neir: "啊实打实",
          img: "../../assets/images/j.png",
          time: new Date(),
          id: 11,
        },
        {
          name: "小是",
          neir: "大萨达",
          img: "../../assets/images/p.png",
          time: new Date(),
          id: 17,
        },
      ],
      roomList: [],
    };
  },

  updated() {
    let msg = this.$el.querySelector("#mainbox>div:last-child"); // 获取对象
    msg.scrollIntoView(); // 滚动高度
  },
  mounted() {},
  created() {
    this.getMsg();
    this.getRoomList();
    this.getMessage();
    this.quit();
    this.welcome();
  },
  destroyed() {
    this.quit();
    console.log("离开了");
  },
  methods: {
    changetime(t) {
      return myfun.dateTime1(t);
    },
    Decode(t) {
      return Base64.decode(t);
    },
    async getRoomList() {
      const { data: res } = await this.$http.get("getRoomList");
      let data = {
        bid: "3",
        name: "dasdsad",
      };
      this.chat.push(data);
      this.chat = res.data;
    },
    onClickLeft() {
      this.$router.push("/Message");
      this.quit();
    },
    selectEmoji(emoji) {
      this.emjon = emoji.aliases;
      console.log(emoji);
      var elInput = document.getElementById("emojiInput"); //获取输入框元素
      // console.log(elInput);
      var start = elInput.selectionStart; // 记录光标开始的位置
      var end = elInput.selectionEnd; // 记录选中的字符 最后的字符的位置
      if (start === undefined || end === undefined) return;
      var txt = elInput.value;
      // 将表情添加到选中的光标位置
      var result = txt.substring(0, start) + emoji.data + txt.substring(end);
      elInput.value = result; // 赋值给input的value
      // 重置光标位置
      elInput.focus();
      elInput.selectionStart = start + emoji.data.length;
      elInput.selectionEnd = start + emoji.data.length;
      console.log(result);
      this.emojitext = result; // 赋值(注意这里一定要赋值给表情输入框绑定的那个值)
    },
    // 发送信息
    async send() {
      let msg = {
        neir: Base64.encode(this.emojitext),
        heardImg: this.emailLogin[0].userImg,
        time: new Date(),
        userID: this.emailLogin[0].userID,
      };
      let data = {
        userName: this.emailLogin[0].Nickname,
        neir: Base64.encode(this.emojitext),
        heardImg: this.emailLogin[0].userImg,
        time: new Date(),
        userID: this.emailLogin[0].userID,
      };

      this.chat.push(msg);
      const { data: res } = await this.$http.post("/addChatMsg", data);
      console.log(res);
      this.$socket.emit("sendMsg", data);
      this.emojitext = "";
      this.showEmojin = false;
      this.$nextTick(() => {
        let msg = this.$el.querySelector("#mainbox>div:last-child"); // 获取对象
        msg.scrollIntoView(); // 滚动高度
      });
    },

    getMsg() {
      this.$socket.emit("join", this.emailLogin[0].Nickname);
    },
    //接收消息
    getMessage() {
      this.sockets.subscribe("msg", (data) => {
        console.log(data);
        this.chat.push(data);
      });
    },
    // 接收用户退出
    quit() {
      this.sockets.subscribe("quit", (name, len) => {
        (this.welMsg = name[0] + "退出群聊"), (this.pel = name[1]);
      });
    },
    //加入聊天室
    welcome() {
      this.sockets.subscribe("welcome", (name, len) => {
        this.welMsg = "欢迎 " + name[0] + " 加入群聊";
        this.biaoji = 1;
        this.pel = name[1];
      });
    },
  },
  computed: {
    ...mapState(["emailLogin"]),
  },
  watch: {},
};
</script>
<style lang="less" scoped>
.nav {
  position: fixed;
  left: 0;
  width: 100%;
  z-index: 1200;
}
.main {
  padding: 40px 0px 0px;
}
.main,
.modfiy-mian {
  .msg {
    display: flex;

    align-items: flex-end;
    padding: 20px 0;
    img {
      flex: none;
      width: 40px;
      height: 40px;
      border-radius: 18px;
      margin: 0 8px;
    }
    .nr {
      .neir {
        padding: 10px 26px;
        min-height: 20px;
        font-size: 14px;
        color: #191d23;
        line-height: 20px;
        max-width: 200px;
      }
      .nt {
        padding-top: 10px;
        position: absolute;
        font-size: 12px;
        color: rgba(25, 29, 35, 0.5);
        line-height: 10px;
      }
    }
  }
  .left {
    flex-direction: row;
    .neir {
      background: rgba(244, 244, 244, 1);
      border-radius: 24px 24px 24px 0px;
    }
  }
  .right {
    flex-direction: row-reverse;
    .neir {
      background: rgba(255, 234, 222, 1);
      border-radius: 24px 24px 0px 24px;
    }
    .nt {
      right: 78px;
    }
  }
  .center {
    text-align: center;
    padding: 20px 0 2px;
    .inner {
      font-size: 14px;
      display: inline-block;
      color: rgba(25, 29, 35, 0.5);
      line-height: 15px;
    }
  }
}
.send {
  position: fixed;
  z-index: 1002;
  bottom: 0;
  width: 100%;
  padding-bottom: 10px;
  min-height: 40px;
  background: rgba(255, 255, 255, 0.96);
  display: flex;
  flex-direction: column;
  .chat-send {
    margin: 4px 40px;
    flex: 1;

    min-height: 21px;
    background: #f2f2f2;
    border-radius: 11px;
    font-size: 15px;
    color: #191d23;
    line-height: 12px;
    padding: 8px;
  }
  .sendimg {
    width: 25px;
    height: 25px;
    position: absolute;
    right: 45px;
    top: 13px;
  }
}
.modify {
  position: fixed;
  overflow: hidden;
  left: 0;
  bottom: -90%;
  width: 100%;
  height: 90%;
  z-index: 1000;
  background: #fff;
  border-radius: 48px 48px 0px 0px;
}
.u-name {
  position: absolute;
  top: 0;
  background-color: #fff;
  height: 80px;
  text-align: center;
  width: 100%;
  line-height: 80px;
}
.modfiy-mian {
  padding: 88px 28px 120px;
  box-sizing: border-box;
  height: 100%;
}
.bg {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  background-color: rgba(0, 0, 0, 0.4);
}
.Picker {
  height: 260px;
}
.emjonBtn {
  width: 25px;
  height: 25px;
  position: absolute;
  top: 13px;
  margin-left: 6px;
}
</style>